<template>
  <div class="app-container">

    <el-table v-loading="loading" :data="noticeList">
      <el-table-column label="语言名称（中文）" align="center" prop="noticeType" width="200"></el-table-column>
      <el-table-column label="语言名称（本国语）" align="center" prop="noticeType" width="200"></el-table-column>
      <el-table-column label="ISO 639 语言代码" align="center" prop="noticeType" width="200"></el-table-column>
      <el-table-column label="状态" align="center" prop="createTime" width="100"></el-table-column>
      <el-table-column label="默认语言" align="center" prop="createTime" width="100"></el-table-column>
      <el-table-column label="添加时间" align="center" prop="createTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <span @click="handleDelete(scope.row)" class="red">删除</span>
          <span v-if="scope.row.status==1" @click="handleLang(scope.row)" class="sanm">启用</span>
          <span v-else @click="handleLang(scope.row)" class="red">禁用</span>
          <span @click="handleUpdate(scope.row)" class="sanm">编辑</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 弹窗 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="语言名称（中文）" prop="noticeType">
              <el-input v-model="form.noticeType" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="语言名称（本国语）" prop="noticeType">
              <el-input v-model="form.noticeType" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="ISO 639 语言代码" prop="noticeType">
              <el-input v-model="form.noticeType" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="是否启用" prop="noticeType">
              <el-checkbox v-model="form.noticeType" /> 启用
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="langTit" :visible.sync="langOpen" width="600px" append-to-body>
      <el-table :data="langDicts">
        <el-table-column label="配置项" align="center" prop="name"></el-table-column>
        <el-table-column label="必填" align="center" prop="flag"></el-table-column>
        <el-table-column label="配置情况" align="center" prop="status"></el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleSet(scope.row)"
            >配置信息</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <el-dialog :title="langTit" :visible.sync="setOpen" width="500px" append-to-body>
      <el-form ref="form" label-width="150px">
          <div class="tit">默认内容</div>
          <el-form-item :label="setCur.name">
            <img v-if="setCur.type==2" :src="setCur.val" style="width:320px; height: 180px;" />
            <div v-else>{{setCur.val}}</div>
          </el-form-item>

          <div class="tit">
            多语言配置 - {{setCur.name}}
            <span class="tip" v-if="setCur.type==2">仅支持jpg、png、jpeg格式，且图片尺寸比例必须为16:9</span>
          </div>
          <el-form-item  v-for="(item, index) in langs" :key="index" :label="item.name">
            <el-input v-if="setCur.type==1" v-model="item.val" placeholder="请输入" />
            <el-upload v-if="setCur.type==2" action="#" :http-request="requestUpload" :show-file-list="false" :before-upload="beforeUpload">
              <el-button size="small">
                上传图片
                <i class="el-icon-upload el-icon--right"></i>
              </el-button>
            </el-upload>
            <editor v-if="setCur.type==3" v-model="item.val" :min-height="192"/>
          </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="setSubmit">确 定</el-button>
        <el-button @click="setCancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="藏品详情" :visible.sync="viewOpen" width="600px" append-to-body>
      <el-form ref="form" label-width="150px">
          <el-form-item label="封面图片">
            <img src="" style="width:150px; height: 150px;" />
          </el-form-item>
          <el-form-item label="藏品名称">
            <span>{{form.noticeType}}</span>
          </el-form-item>
          <el-form-item label="藏品类型">
            <span>{{form.noticeType}}</span>
          </el-form-item>
          <el-form-item label="所属朝代">
            <span>{{form.noticeType}}</span>
          </el-form-item>
          <el-form-item label="藏品介绍">
            <span>{{form.noticeType}}</span>
          </el-form-item>
          <el-form-item label="状态">
            <span>{{form.noticeType}}</span>
          </el-form-item>
          <el-form-item label="馆藏精品">
            <span>{{form.noticeType}}</span>
          </el-form-item>
          <el-form-item label="镇馆之宝">
            <span>{{form.noticeType}}</span>
          </el-form-item>
          <el-form-item label="藏品展示">
            <img v-for="(item, index) in 9" :key="index" src="" style="width:100px; height:100px; margin: 0 5px 5px 0;" />
          </el-form-item>

      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import { listNotice, getNotice, delNotice, addNotice, updateNotice } from "@/api/system/notice";

export default {
  name: "Notice",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      noticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        noticeTitle: [
          { required: true, message: "公告标题不能为空", trigger: "blur" }
        ]
      },
      daterangeCreateTime: [],

      viewOpen: false,

      //多语言设置
      langOpen: false,
      langTit: '多语言配置',
      //type 1输入框 2图片上传 3富文本
      langDicts: [
        {id: 2, name: '名称', status: 0, flag: 1, type: 1, val: 'http://www.baidu.com'}
      ],
      setCur: {},
      setOpen: false,
      langs: [
        {id: 1, name: '中文（简体）', val: ''},
        {id: 2, name: '中文（繁体）', val: ''},
        {id: 3, name: '英语（English）', val: ''}
      ]

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      listNotice(this.queryParams).then(response => {
        this.noticeList = response.data.list || [];
        this.total = response.data.total || 0;
        this.loading = false;
      });
    },
    handleLang(row){
      this.langTit = '藏品' + ' - 多语言配置';
      this.langOpen = true;
    },
    handleSet(row){
      this.setCur = row;
      this.setOpen = true;
    },
    setSubmit(){
      addNotice(this.form).then(response => {
        this.$modal.msgSuccess("设置成功");
        this.setOpen = false;
        this.getList();
      });
    },
    setCancel(){
      this.setOpen = false;
    },
    handleView(row){
      this.form = row;
      this.viewOpen = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        noticeId: undefined,
        noticeTitle: undefined,
        noticeType: undefined,
        noticeContent: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.noticeId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增藏品类型";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const noticeId = row.noticeId || this.ids
      getNotice(noticeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "编辑藏品类型";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.noticeId != undefined) {
            updateNotice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNotice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const noticeIds = row.noticeId || this.ids
      this.$modal.confirm('是否确认删除公告编号为"' + noticeIds + '"的数据项？').then(function() {
        return delNotice(noticeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 覆盖默认的上传行为
    requestUpload() {
    },
    // 向左旋转
    rotateLeft() {
      this.$refs.cropper.rotateLeft();
    },
    // 向右旋转
    rotateRight() {
      this.$refs.cropper.rotateRight();
    },
    // 图片缩放
    changeScale(num) {
      num = num || 1;
      this.$refs.cropper.changeScale(num);
    },
    // 上传预处理
    beforeUpload(file) {
      if (file.type.indexOf("image/") == -1) {
        this.$modal.msgError("文件格式错误，请上传图片类型,如：JPG，PNG后缀的文件。");
      } else {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = () => {
          this.options.img = reader.result;
          this.options.filename = file.name;
        };
      }
    },
    // 上传图片
    uploadImg() {
      this.$refs.cropper.getCropBlob(data => {
        let formData = new FormData();
        console.log(this.options.filename)
        formData.append("avatarfile", data, this.options.filename);
        uploadAvatar(formData).then(response => {
          this.open = false;
          this.options.img = response.data.imgUrl;
          store.commit('SET_AVATAR', this.options.img);
          this.$modal.msgSuccess("修改成功");
          this.visible = false;
        });
      });
    }
  }
};
</script>